import React,{Component} from 'react'
import {browserHistory,Link} from 'react-router';
import * as pub from '../../config/public';
import {message} from 'antd';
import * as fetch from '../../config/fetch';
class Header extends Component {
	constructor(args) {
		super();
		this.state={
			userRealName:'',
			job:[]
		}
	}
	exit() {
		pub.exit();
	}
	selectRealname(){
		fetch.selectRealname({}).then((data)=>{
			if(data.result==='RC100'){
				this.setState({
					userRealName:data.userRealName
				})
			}else{
				message.error(data.errMsg, 3);
			}
		}, (res) => {
			pub.reject(res);
		})
	}
	componentWillMount() {
		this.selectRealname();
		if(pub.user === null){
			pub.jumpHistory.push('/Login');
			return;
		}
		if (localStorage.getItem("now_userType") !== '管理员') {
			pub.jumpHistory.push('/Entrance');
			return;
		}
		let job=[];
		pub.user.job.map((item,index)=>{
			if(item!=='管理员')job.push(({
				name:item,
				to:item==='学员'?'/':'/CourseManage'
			}));
			return true;
		})
		this.setState({
			job
		})
	}
	change(e){
		localStorage.setItem("now_userType", e.target.text)
	}
	render(){
		return(
		      <div className="header">
		      	<lable className="logo"> </lable>
		      	<span>运营中心后台管理</span>
		      	<div className="header-right">
		      		<span>管理员：{this.state.userRealName}</span>
		      		{this.state.job.map((item,index)=>{
		      			return(
		      				<Link onClick={(e)=>this.change(e)} key={index} to={item.to}>{item.name}</Link>
		      				)
		      		})}
		      		<a onClick={()=>this.exit()} >注销</a>
		      	</div>
		      </div>
			)
	}
}
export default Header;